﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

public partial class downloadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string fileName = System.Configuration.ConfigurationManager.AppSettings["ClientDownFileName"];//客户端保存的文件名
        ////Create a stream for the file
        //Stream stream = null;

        ////This controls how many bytes to read at a time and send to the client
        //int bytesToRead = 100000;

        //// Buffer to read bytes in chunk size specified above
        //byte[] buffer = new Byte[bytesToRead];

        //// The number of bytes read
        //try
        //{
        //    string url = "http://lattissima.ogilvy.com.cn/downloadfiles/Voucher_cn.pdf";
        //    //Create a WebRequest to get the file
        //    HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);

        //    //Create a response for this request
        //    HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

        //    if (fileReq.ContentLength > 0)
        //        fileResp.ContentLength = fileReq.ContentLength;

        //    //Get the Stream returned from the response
        //    stream = fileResp.GetResponseStream();

        //    // prepare the response to the client. resp is the client Response

        //    //Indicate the type of data being sent
        //    Response.ContentType = "application/octet-stream";

        //    //Name the file 
        //    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        //    Response.AddHeader("Content-Length", fileResp.ContentLength.ToString());

        //    int length;
        //    do
        //    {
        //        // Verify that the client is connected.
        //        if (Response.IsClientConnected)
        //        {
        //            // Read data into the buffer.
        //            length = stream.Read(buffer, 0, bytesToRead);

        //            // and write it out to the response's output stream
        //            Response.OutputStream.Write(buffer, 0, length);

        //            // Flush the data
        //            Response.Flush();

        //            //Clear the buffer
        //            buffer = new Byte[bytesToRead];
        //        }
        //        else
        //        {
        //            // cancel the download if client has disconnected
        //            length = -1;
        //        }
        //    } while (length > 0); //Repeat until no data is read
        //}
        //finally
        //{
        //    if (stream != null)
        //    {
        //        //Close the input stream
        //        stream.Close();
        //    }
        //}



        string fileName = System.Configuration.ConfigurationManager.AppSettings["ClientDownFileName"];//客户端保存的文件名
        string flag = Request.QueryString["lang"];
        string filePath;
        if (flag == "cn")
            filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["CnDownFilePath"]);
        else
            filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["EnDownFilePath"]);//路径

        //以字符流的形式下载文件
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}